#include <stdio.h>

#include "tlib_dbf.h"

main()
{
    int iDbfHandle;
    long lRecordCount;
    char sFreqstu[2];
    char sFreqtype[2];
    char sFackmsg[20];
    char sFstnid[20];
    char sFmsgnum[4];
    int i, iError;

    iDbfHandle = TLib_Dbf_Open("t_req.dbf", TLIB_DBF_OPEN_MODE_WRITE);
    if (iDbfHandle < 0)
    {
        printf("Fail To Open File(%d)\n", iDbfHandle);
        return -1;
    }

    lRecordCount = TLib_Dbf_RecordCount(iDbfHandle);

    for (i = 1; i <= lRecordCount/2; i++)
    {
        iError = 0;
        iError |= TLib_Dbf_Go(iDbfHandle, i);
        iError |= TLib_Dbf_LoadRecord(iDbfHandle);
        iError |= TLib_Dbf_ReadField(iDbfHandle, "Freqstu", sFreqstu, sizeof(sFreqstu));
        iError |= TLib_Dbf_ReadField(iDbfHandle, "Freqtype", sFreqtype, sizeof(sFreqtype));
        iError |= TLib_Dbf_ReadField(iDbfHandle, "Fackmsg", sFackmsg, sizeof(sFackmsg));
        iError |= TLib_Dbf_ReadField(iDbfHandle, "Fstnid", sFstnid, sizeof(sFstnid));
        iError |= TLib_Dbf_ReadField(iDbfHandle, "Fmsgnum", sFmsgnum, sizeof(sFmsgnum));
        if (iError != 0)
        {
            printf("Fail To Read Record\n");
            break;
        }
        if (TLib_Dbf_Deleted(iDbfHandle))
           printf("D-%d-%s-%s-%s-%s-%s\n", i, sFreqstu, sFreqtype, sFackmsg, sFstnid, sFmsgnum);
        else
           printf("U-%d-%s-%s-%s-%s-%s\n", i, sFreqstu, sFreqtype, sFackmsg, sFstnid, sFmsgnum);
    }

    for (i = lRecordCount/2+1; i <= lRecordCount; i++)
    {
        iError = 0;
        iError |= TLib_Dbf_Go(iDbfHandle, i);
        iError |= TLib_Dbf_LoadRecord(iDbfHandle);

        sprintf(sFmsgnum, "%d", i);
        iError |= TLib_Dbf_ReplaceField(iDbfHandle, "Fmsgnum", sFmsgnum);
        iError |= TLib_Dbf_ReplaceField(iDbfHandle, "Fackmsg", "123456789012345678901234567890123456789012345678901234567890");
        iError |= TLib_Dbf_ReplaceField(iDbfHandle, "Fstnid", "ABC");
        iError |= TLib_Dbf_ReplaceField(iDbfHandle, "Freqtype", "b");
        iError |= TLib_Dbf_ReplaceField(iDbfHandle, "Freqstu", "a");

        iError |= TLib_Dbf_Go(iDbfHandle, i);
        iError |= TLib_Dbf_CommitRecord(iDbfHandle);
        if (iError != 0)
        {
            printf("Fail To Write Record\n");
            break;
        }
    }

    for (i = 1; i <= 10; i++)
    {
        iError = 0;
        iError |= TLib_Dbf_ClearRecordBuffer(iDbfHandle);

        sprintf(sFmsgnum, "%d", i);
        iError |= TLib_Dbf_ReplaceField(iDbfHandle, "Fmsgnum", sFmsgnum);
        iError |= TLib_Dbf_ReplaceField(iDbfHandle, "Fackmsg", "aaabbbcccdddeeefffggghhhiiijjjkkklllmmmnnnooopppqqqrrrsssttt");
        iError |= TLib_Dbf_ReplaceField(iDbfHandle, "Fstnid", "123");
        iError |= TLib_Dbf_ReplaceField(iDbfHandle, "Freqtype", "1");
        iError |= TLib_Dbf_ReplaceField(iDbfHandle, "Freqstu", "2");

        if (i % 2)
            TLib_Dbf_Delete(iDbfHandle);
        else
            TLib_Dbf_Undelete(iDbfHandle);

        iError |= TLib_Dbf_CommitAppend(iDbfHandle);
        if (iError != 0)
        {
            printf("Fail To Write Record\n");
            break;
        }
    }

    TLib_Dbf_Close(iDbfHandle);
    return 0;
}